﻿using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using LTJ.Config;
using LTJ.Common;
using LTJ.Common.XML;
using LTJ.Common.Generic;
using LTJ.Cache;
using LTJ.Entity;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public class ManageMenu
    {
        private static readonly string adminconfigPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "config/adminmenu.config");
        private static readonly string userconfigPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "config/usermenu.config");

        #region 菜单管理

        /// <summary>
        /// 增加系统顶部菜单
        /// </summary>
        /// <param name="title">顶部菜单标题</param>
        /// <param name="defaulturl">顶部菜单默认展开的页面</param>
        /// <returns>新顶部菜单项ID</returns>
        public static int NewAdminTopMenu(string title, string defaulturl)
        {
            return NewTopMenu(title, defaulturl, 1);
        }
        /// <summary>
        /// 增加用户顶部菜单
        /// </summary>
        /// <param name="title">顶部菜单标题</param>
        /// <param name="defaulturl">顶部菜单默认展开的页面</param>
        /// <returns>新顶部菜单项ID</returns>
        public static int NewUserTopMenu(string title, string defaulturl)
        {
            return NewTopMenu(title, defaulturl, 2);
        }
        /// <summary>
        /// 增加顶部菜单
        /// </summary>
        /// <param name="title">顶部菜单标题</param>
        /// <param name="defaulturl">顶部菜单默认展开的页面</param>
        /// <returns>新顶部菜单项ID</returns>
        public static int NewTopMenu(string title, string defaulturl, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList mainmenus = doc.SelectNodes("/dataset/toptabmenu");
            int newMenuId = mainmenus.Count + 1;
            XmlElement newMainMenuItem = doc.CreateElement("toptabmenu");
            XmlElement node = doc.CreateElement("id");
            node.InnerText = newMenuId.ToString();
            newMainMenuItem.AppendChild(node);

            node = doc.CreateElement("title");
            node.InnerText = title;
            newMainMenuItem.AppendChild(node);

            node = doc.CreateElement("mainmenulist");
            node.InnerText = "";
            newMainMenuItem.AppendChild(node);

            node = doc.CreateElement("mainmenuidlist");
            node.InnerText = "";
            newMainMenuItem.AppendChild(node);

            node = doc.CreateElement("defaulturl");
            node.InnerText = defaulturl;
            newMainMenuItem.AppendChild(node);

            node = doc.CreateElement("system");
            node.InnerText = "0";
            newMainMenuItem.AppendChild(node);

            doc.SelectSingleNode("/dataset").AppendChild(newMainMenuItem);
            doc.Save(savepath);
            return newMenuId;
        }

        /// <summary>
        /// 新建系统主菜单
        /// </summary>
        /// <param name="menuid">上级菜单ID</param>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="etitle">英文标题</param>
        /// <returns>新建菜单ID</returns>
        public static int NewAdminMainMenu(int menuid, string menutitle, string etitle)
        {
            return NewMainMenu(menuid, menutitle, etitle, 1);
        }

        /// <summary>
        /// 新建用户主菜单
        /// </summary>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="defaluturl">默认地址</param>
        /// <param name="etitle">英文标题</param>
        /// <returns>新建菜单ID</returns>
        public static int NewUserMainMenu(int menuid, string menutitle, string etitle)
        {
            return NewMainMenu(menuid, menutitle, etitle, 2);
        }

        /// <summary>
        /// 新建主菜单
        /// </summary>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="defaluturl">默认地址</param>
        /// <param name="etitle">英文标题</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        /// <returns>新建菜单ID</returns>
        public static int NewMainMenu(int mainmenuid, string menutitle, string etitle, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            int newid = 1;
            int newmenuid = 100;
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/mainmenu");
            newid += int.Parse(submains.Item(submains.Count - 1)["id"].InnerText);
            newmenuid += int.Parse(submains.Item(submains.Count - 1)["menuid"].InnerText);
            XmlElement mainmenu = doc.CreateElement("mainmenu");
            XmlElement node = doc.CreateElement("id");
            node.InnerText = newid.ToString();
            mainmenu.AppendChild(node);

            node = doc.CreateElement("menuid");
            node.InnerText = newmenuid.ToString();
            mainmenu.AppendChild(node);

            node = doc.CreateElement("menutitle");
            node.InnerText = menutitle;
            mainmenu.AppendChild(node);

            node = doc.CreateElement("englishname");
            node.InnerText = etitle;
            mainmenu.AppendChild(node);

            doc.SelectSingleNode("/dataset").AppendChild(mainmenu);

            XmlNodeList mainMenus = doc.SelectNodes("/dataset/toptabmenu");
            foreach (XmlNode menuItem in mainMenus) //查找主菜单信息
            {
                if (menuItem["id"].InnerText == mainmenuid.ToString())
                {
                    menuItem["mainmenulist"].InnerText += "," + newid;
                    menuItem["mainmenuidlist"].InnerText += "," + newmenuid;
                    menuItem["mainmenulist"].InnerText = menuItem["mainmenulist"].InnerText.TrimStart(',');
                    menuItem["mainmenuidlist"].InnerText = menuItem["mainmenuidlist"].InnerText.TrimStart(',');
                    break;
                }
            }
            doc.Save(savepath);
            return newmenuid;
        }

        /// <summary>
        /// 添加系统管理子菜单
        /// </summary>
        /// <param name="menuparentid">上级菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        public static bool NewAdminMenuItem(int menuparentid, string menutitle, string linkurl, string etitle, int opvalue)
        {
            return NewMenuItem(menuparentid, menutitle, linkurl, etitle, opvalue, 1);
        }

        /// <summary>
        /// 添加用户管理子菜单
        /// </summary>
        /// <param name="menuparentid">上级菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        public static bool NewUserMenuItem(int menuparentid, string menutitle, string linkurl, string etitle, int opvalue)
        {
            return NewMenuItem(menuparentid, menutitle, linkurl, etitle, opvalue, 2);
        }

        /// <summary>
        /// 添加子菜单
        /// </summary>
        /// <param name="menuparentid">上级菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        public static bool NewMenuItem(int menuparentid, string menutitle, string linkurl, string etitle, int opvalue, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/submain");
            foreach (XmlNode sub in submains)
            {
                if (sub["link"].InnerText == linkurl)
                {
                    return false;
                }
            }
            XmlElement submain = doc.CreateElement("submain");
            XmlElement node = doc.CreateElement("menuparentid");
            node.InnerText = menuparentid.ToString();
            submain.AppendChild(node);

            node = doc.CreateElement("menutitle");
            node.InnerText = menutitle;
            submain.AppendChild(node);

            node = doc.CreateElement("link");
            node.InnerText = linkurl;
            submain.AppendChild(node);

            node = doc.CreateElement("frameid");
            node.InnerText = opvalue.ToString();
            submain.AppendChild(node);

            node = doc.CreateElement("englishname");
            node.InnerText = etitle;
            submain.AppendChild(node);

            doc.SelectSingleNode("/dataset").AppendChild(submain);
            doc.Save(savepath);
            return true;
        }

        /// <summary>
        /// 编辑系统顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        /// <param name="menutitle">菜单标题</param>
        /// <param name="menudefaultlink">菜单默认链接</param>
        public static bool EditAdminTopMenu(int menuid, string menutitle, string menudefaultlink)
        {
            return EditTopMenu(menuid, menutitle, menudefaultlink, 1);
        }

        /// <summary>
        /// 编辑用户顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        /// <param name="menutitle">菜单标题</param>
        /// <param name="menudefaultlink">菜单默认链接</param>
        public static bool EditUserTopMenu(int menuid, string menutitle, string menudefaultlink)
        {
            return EditTopMenu(menuid, menutitle, menudefaultlink, 2);
        }

        /// <summary>
        /// 编辑顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        /// <param name="menutitle">菜单标题</param>
        /// <param name="menudefaultlink">菜单默认链接</param>
        /// <param name="menutype">类型</param>
        public static bool EditTopMenu(int menuid, string menutitle, string menudefaultlink, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList mainmenus = doc.SelectNodes("/dataset/toptabmenu");
            bool result = false;
            foreach (XmlNode menuitem in mainmenus)
            {
                if (menuitem["id"].InnerText == menuid.ToString())
                {
                    menuitem["title"].InnerText = menutitle;
                    menuitem["defaulturl"].InnerText = menudefaultlink;
                    result = true;
                    break;
                }
            }
            if (result)
            {
                doc.Save(savepath);
            }
            return result;
        }

        /// <summary>
        /// 编辑系统管理主菜单
        /// </summary>
        /// <param name="submenuid">主菜单ID</param>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="defaluturl">默认地址</param>
        /// <param name="etitle">英文标题</param>
        /// <returns>操作成功否</returns>
        public static bool EditAdminMainMenu(int submenuid, string menutitle, string etitle)
        {
            return EditMainMenu(submenuid, menutitle, etitle, 1);
        }
        /// <summary>
        /// 编辑用户管理主菜单
        /// </summary>
        /// <param name="submenuid">主菜单ID</param>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="defaluturl">默认地址</param>
        /// <param name="etitle">英文标题</param>
        /// <returns>操作成功否</returns>
        public static bool EditUserMainMenu(int submenuid, string menutitle, string etitle)
        {
            return EditMainMenu(submenuid, menutitle, etitle, 2);
        }

        /// <summary>
        /// 编辑管理主菜单
        /// </summary>
        /// <param name="submenuid">主菜单ID</param>
        /// <param name="menutitle">主菜单标题</param>
        /// <param name="defaluturl">默认地址</param>
        /// <param name="etitle">英文标题</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        public static bool EditMainMenu(int submenuid, string menutitle, string etitle, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/mainmenu");
            bool result = false;
            foreach (XmlNode menuItem in submains)
            {
                if (menuItem["id"].InnerText == submenuid.ToString())
                {
                    menuItem["menutitle"].InnerText = menutitle;
                    menuItem["englishname"].InnerText = etitle;
                    result = true;
                    break;
                }
            }
            if (result)
            {
                doc.Save(savepath);
            }
            return result;
        }

        /// <summary>
        /// 编辑系统子菜单
        /// </summary>
        /// <param name="subid">菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        public static bool EditAdminMenuItem(int subid, string menutitle, string linkurl, string etitle, int opvalue)
        {
            return EditMenuItem(subid, menutitle, linkurl, etitle, opvalue, 1);
        }

        /// <summary>
        /// 编辑用户子菜单
        /// </summary>
        /// <param name="subid">菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        public static bool EditUserMenuItem(int subid, string menutitle, string linkurl, string etitle, int opvalue)
        {
            return EditMenuItem(subid, menutitle, linkurl, etitle, opvalue, 2);
        }

        /// <summary>
        /// 编辑子菜单
        /// </summary>
        /// <param name="subid">菜单ID</param>
        /// <param name="menutitle">菜单名</param>
        /// <param name="etitle">英文名</param>
        /// <param name="linkurl">链接页面</param>
        /// <param name="opvalue">操作实体值</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        public static bool EditMenuItem(int subid, string menutitle, string linkurl, string etitle, int opvalue, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/submain");
            int rowcount = 0;
            foreach (XmlNode sub in submains)
            {
                if (rowcount.ToString() != subid.ToString() && sub["link"].InnerText == linkurl)
                {
                    return false;
                }
                rowcount++;
            }
            submains.Item(subid)["menutitle"].InnerText = menutitle;
            submains.Item(subid)["link"].InnerText = linkurl;
            submains.Item(subid)["englishname"].InnerText = etitle;
            submains.Item(subid)["frameid"].InnerText = opvalue.ToString();
            doc.Save(savepath);
            return true;
        }

        /// <summary>
        /// 删除系统顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        public static bool DeleteAdminTopMenu(int menuid)
        {
            return DeleteTopMenu(menuid, 1);
        }

        /// <summary>
        /// 删除用户顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        public static bool DeleteUserTopMenu(int menuid)
        {
            return DeleteTopMenu(menuid, 2);
        }

        /// <summary>
        /// 删除顶部菜单
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        /// <param name="menutype">类型</param>
        public static bool DeleteTopMenu(int menuid, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList mainmenus = doc.SelectNodes("/dataset/toptabmenu");
            XmlNode delMenu = null;
            int newid = menuid;
            bool result = false;
            foreach (XmlNode menuitem in mainmenus)
            {
                if (menuitem["id"].InnerText == menuid.ToString())
                {
                    if (menuitem["mainmenulist"].InnerText.Trim() != "")
                        return false;
                    delMenu = menuitem;
                    result = true;
                    break;
                }
                else
                {
                    if (delMenu != null)
                    {
                        menuitem["id"].InnerText = newid.ToString();
                        newid++;
                    }
                }
            }
            if (result)
            {
                delMenu.ParentNode.RemoveChild(delMenu);
                doc.Save(savepath);
            }
            return result;
        }

        /// <summary>
        /// 删除管理主菜单
        /// </summary>
        /// <param name="submenuid">子菜单ID</param>
        /// <param name="mainmenuid">主菜单ID</param>
        public static bool DeleteAdminMainMenu(int submenuid, int mainmenuid)
        {
            return DeleteMainMenu(submenuid, mainmenuid, 1);
        }

        /// <summary>
        /// 删除用户主菜单
        /// </summary>
        /// <param name="submenuid">子菜单ID</param>
        /// <param name="mainmenuid">主菜单ID</param>
        public static bool DeleteUserMainMenu(int submenuid, int mainmenuid)
        {
            return DeleteMainMenu(submenuid, mainmenuid, 2);
        }

        /// <summary>
        /// 删除管理主菜单
        /// </summary>
        /// <param name="submenuid">子菜单ID</param>
        /// <param name="mainmenuid">主菜单ID</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        public static bool DeleteMainMenu(int submenuid, int mainmenuid, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;

            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/mainmenu");
            bool result = false;
            string menuid = "";
            foreach (XmlNode menuItem in submains)
            {
                if (menuItem["id"].InnerText == submenuid.ToString())
                {
                    menuid = menuItem["menuid"].InnerText;
                    XmlNodeList items = doc.SelectNodes("/dataset/submain");
                    foreach (XmlNode item in items)
                    {
                        if (item["menuparentid"].InnerText == menuid)
                            return false;
                    }
                    menuItem.ParentNode.RemoveChild(menuItem);
                    result = true;
                    break;
                }
            }
            XmlNodeList mainMenus = doc.SelectNodes("/dataset/toptabmenu");
            foreach (XmlNode menuItem in mainMenus) //查找主菜单信息
            {
                if (menuItem["id"].InnerText == mainmenuid.ToString())
                {
                    menuItem["mainmenulist"].InnerText = ("," + menuItem["mainmenulist"].InnerText + ",").Replace("," + submenuid + ",", ",");
                    menuItem["mainmenuidlist"].InnerText = ("," + menuItem["mainmenuidlist"].InnerText + ",").Replace("," + menuid + ",", ",");
                    menuItem["mainmenulist"].InnerText = menuItem["mainmenulist"].InnerText.TrimStart(',').TrimEnd(',');
                    menuItem["mainmenuidlist"].InnerText = menuItem["mainmenuidlist"].InnerText.TrimStart(',').TrimEnd(',');
                    break;
                }
            }
            if (result)
            {
                doc.Save(savepath);
            }
            return result;
        }

        /// <summary>
        /// 删除系统管理子菜单
        /// </summary>
        /// <param name="itemid">菜单ID</param>
        public static void DeleteAdminMenuItem(int itemid)
        {
            DeleteMenuItem(itemid, 1);
        }

        /// <summary>
        /// 删除用户子菜单
        /// </summary>
        /// <param name="itemid">菜单ID</param>
        public static void DeleteUserMenuItem(int itemid)
        {
            DeleteMenuItem(itemid, 2);
        }

        /// <summary>
        /// 删除子菜单
        /// </summary>
        /// <param name="itemid">菜单ID</param>
        /// <param name="menutype">菜单类型1系统菜单，2用户菜单</param>
        public static void DeleteMenuItem(int itemid, int menutype)
        {
            string savepath = "";

            if (menutype == 1) savepath = adminconfigPath;
            else savepath = userconfigPath;
            XmlDocumentExtender doc = new XmlDocumentExtender();
            doc.Load(savepath);
            XmlNodeList submains = doc.SelectNodes("/dataset/submain");
            string link = submains.Item(itemid)["link"].InnerText;
            submains.Item(itemid).ParentNode.RemoveChild(submains.Item(itemid));
            doc.Save(savepath);
        }

        #endregion
    }
}
